<?php


namespace App\Service;


use App\Facades\Site;
use App\Lib\Common;
use App\Models\Admin\AdminUser;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\View\View;
use App\Models\Admin\Menu as MenuModel;


class Menu
{
    /*获取菜单列表*/
    public function compose(View $v) {
        //获取全部菜单
        $data = (new MenuModel())->getAll()->toArray();
        //处理参数信息
        $data = (new MenuService())->params($data);

        //获取用户权限
        $user = (new AdminUser())->find(getCurrentUser()["id"]);
        $permissions = $user->getAllPermissions()->toArray();

        //菜单权限处理
        $list = $this->menuList($data, $permissions);

        //处理父级菜单
        $temp = Common::father_list($list, $data);

        //生成菜单树
        $tree = Common::getTree(array_unique(array_merge($list, $temp), SORT_REGULAR));
        //对树形排序，正序
        if (!empty($tree)) {
            if(getCurrentSiteId()>0){
                //合并站点自定义菜单
                $tree = $this->makeLink($tree);
            }
        }


        //控制菜单选中效果
        $currRouteName = Route::currentRouteName();
        $cache_key = 'menu_route_' . getCurrentSiteId() . getCurrentUser()["id"];
        if (MenuModel::where('uri', $currRouteName)->count() > 0) {
            //当前路由为菜单
            Cache::put($cache_key, $currRouteName, 120);
        } else {
            if (Cache::has($cache_key)) {
                $currRouteName = Cache::get($cache_key);
            }
        }

        //特效展示
        $temp = explode(".",$currRouteName);
        $strs = [$temp[0],$temp[1],'index'];


        //获取面包屑
        $nav = (new MenuModel())->getPosition($currRouteName);
        //传递数据
        $v->with(["menu" => $tree, "currRouteName" => implode('.',$strs), "position" => $nav,"navName"=>Site::config('site.name')]);
    }

    //菜单权限处理
    private function menuList($data, $permissions) {
        $list = [];
        for ($i = 0; $i < count($data); $i++) {
            if ($data[$i]["is_link"] == 1) {
                $list[] = $data[$i];
            } else {
                for ($j = 0; $j < count($permissions); $j++) {
                    if ($permissions[$j]["id"] == $data[$i]["permission_id"]) {
                        $list[] = $data[$i];
                    }
                }
            }
        }
        return $list;
    }

    //处理菜单中带有超链接的菜单
    private function makeLink($trees) {
        $data = ["id" => 999999, "parent_id" => 0, "icon" => "layui-icon-user", "uri" => "", "is_link" => 0, "name" => "自定义菜单", "site_id" => getCurrentSiteId(), "children" => []];
        //删除的序号
        $idx=[];
        //记录是否有链接
        $flag=0;
        for ($i = 0; $i < count($trees); $i++) {
            if ($trees[$i]["is_link"] == 1) {
                $data["children"][] = $trees[$i];
                $idx[]=$i;
                $flag++;
            }
        }
        if($flag>0){
            $trees[]=$data;
            return $this->removeMuen($idx,$trees);
        }
        return $trees;

    }

    //移除多余链接菜单
    private function removeMuen($idx,$arr){
        foreach ($idx as $k=>$v){
            unset($arr[$v]);
        }
        return array_values($arr);
    }


}
